<?
namespace Admin;

use AdminController;
use Config;
use Input;
use Lang;
use MyModel;
use Redirect;
use Sentry;
use Validator;
use Vcategory;
use Video;
use View;


class VideosController extends AdminController
{
    public function getIndexCategory()
    {
        $data['categories'] = VCategory::all();
        return View::make('backend/videos/categories/index', compact('data'));
    }

    public function getCreateCategory()
    {
        $data['category'] = new Vcategory();
        return View::make('backend/videos/categories/edit', compact('data'));
    }

    public function getEditCategory($id)
    {

    }

    public function postCreateCategory()
    {

    }

    public function getDeleteCategory($id)
    {

    }

    public function getRestoryCategory($id)
    {

    }

    public function getIndex()
    {
        $data['appends']['k'] = Input::get('k', '');
        $data['appends']['withTrashed'] = Input::get('withTrashed', false);
        $data['appends']['onlyTrashed'] = Input::get('onlyTrashed', false);
        $data['appends']['category'] = Input::get('category', '');
        $data['appends']['order'] = Input::get('order', 'id');
        $videos = new Video();

        if ($data['appends']['withTrashed'])
            $videos = $videos->withTrashed();

        if ($data['appends']['onlyTrashed'])
            $videos = $videos->onlyTrashed();

        $data['Videos'] = $videos->leftJoin('categories', 'categories.id', '=', 'videos.category_id')
            ->where(function ($where) use ($data) {
                if ($data['appends']['category'] != '') {
                    $where->where('category_id', '=', $data['appends']['category']);
                }
                if ($data['appends']['k'] != '') {
                    $where->where('videos.title', 'LIKE', '%' . $data['appends']['k'] . '%');
                }
            })
            ->select('videos.*', 'categories.title as c_title')
            ->orderBy($data['appends']['order'], 'DESC')->paginate()->appends($data['appends']);
        $myModel = new MyModel();
        $data['categories'] = $myModel->listAllItemsToArray('Category', array('value' => 'id', 'display' => 'title'));
        return View::make('backend/videos/index', compact('data'));
    }

    public function getCreate()
    {

    }

    public function getEdit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['video', 'video.edit']))
            return View::make('backend/notallow');
        if (is_null($data['video'] = Video::find($id)))
            return Redirect::route('videos')->with('error', '');
        $data['tags'] = $data['video']->tags()->select('tags.*', 'taggable.id as t_id')->get();

        $data['topicables'] = array();
        $topicables = $data['video']->topics()->select('topic_id')->get();
        foreach ($topicables as $topicable) {
            $data['topicables'][] = $topicable->topic_id;
        }

        $myModel = new \MyModel();

        $data['topics'] = \Topic::returnArray();
        $data['categories'] = $myModel->listAllItemsToArray('Category', array('value' => 'id', 'display' => 'title'));
        $data['tones'] = $myModel->listAllItemsToArray('Tone', array('value' => 'id', 'display' => 'title'));
        $data['event'] = $data['video']->contest()->first();
        return View::make('backend/videos/edit', compact('data'));
    }

    public function postCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['video', 'video.edit']))
            return View::make('backend/notallow');
        $data = Input::get('data');
        $rules = array('title' => 'required', 'youtube' => 'required');
        $validation = Validator::make($data['Video'], $rules);
        if ($validation->fails())
            return Redirect::back()->withInput()->withErrors($validation);
        if (!empty($data['Video']['id']))
            $data['Video']['user_id'] = Sentry::getUser()->id;
        $myModel = new MyModel();
        $video = $myModel->saveItem('Video', $data['Video'], array('return' => 'class'));
        if (isset($data['Topics'])) {
            $video->topics()->detach();
            $video->topics()->attach($data['Topics']);
        }
        if (isset($data['Tags']) && !empty($data['Tags'])) {
            $tags = explode(',', $data['Tags']);
            foreach ($tags as $tag) {
                $tag_id = \Tag::Check($tag);
                $video->tags()->attach($tag_id);
            }
        }

        return Redirect::route('update/video/admin', $video->id)->with('success', 'Hợp âm đã được lưu');


    }

    public function getDelete($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['video', 'video.delete']))
            return View::make('backend/notallow');
        if (is_null($video = Video::find($id)))
            return Redirect::route('videos')->with('error', '');
        $video->delete();
        return Redirect::route('videos')->with('success', '');
    }

    public function getRestore($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['video', 'video.delete']))
            return View::make('backend/notallow');
        if (is_null($video = Video::withTrashed()->find($id)))
            return Redirect::route('videos')->with('error', '');
        $video->restore();
        return Redirect::route('videos')->with('success', '');
    }
}